package com.karaoke.android.data.service

import com.karaoke.android.data.domain.PlayList
import com.karaoke.android.data.domain.Song
import com.karaoke.android.data.krealmextensions.query
import com.karaoke.android.data.krealmextensions.queryFirst
import com.karaoke.android.data.krealmextensions.save
import com.karaoke.android.data.krealmextensions.saveAll
import org.simple.eventbus.EventBus

/**
 * Created by allen on 2017/4/29.
 */
object MediaListService
{
    val mediaList: PlayList by lazy {
        val list = PlayList()
        val realmList = list.queryFirst()
        if (realmList == null)
        {
            list.save()
        }
        else
        {
            realmList.endList.clear()
            realmList.save()
        }
        list.queryFirst()!!
    }

    private var publicSongIndex = 0

    var publicSongs = Song().query { it.equalTo("isPublic", true) }
        set(value)
        {
            field = value
            field.saveAll()
        }

    @JvmStatic
    @Synchronized
    fun onAdd(song: Song)
    {
        if (!mediaList.list.contains(song))
        {
            mediaList.list.add(song)
            onSave()
            if (mediaList.current?.isTemp ?: false)
            {
                nextPlay()
            }
        }
    }

    @JvmStatic
    @Synchronized
    fun onUp(song: Song)
    {
        if (mediaList.list.contains(song))
            mediaList.list.remove(song)
        mediaList.list.add(0, song)
        onSave()
        if (mediaList.current?.isTemp ?: false)
        {
            nextPlay()
        }
    }

    @JvmStatic
    @Synchronized
    fun onDelete(song: Song)
    {
        if (mediaList.list.contains(song))
        {
            mediaList.list.remove(song)
            onSave()
        }
    }

    fun onSave()
    {
        mediaList.save()
        EventBus.getDefault().post(mediaList)
    }

    /**
     * 下一首播放
     */
    fun nextPlay(): Song?
    {
        mediaList.current?.let { mediaList.endList.add(mediaList.current) }
        mediaList.current = null
        if (mediaList.list.isNotEmpty())
        {
            val s = mediaList.list.removeAt(0)
            mediaList.current = s
            onSave()
            return s
        }
        else if (publicSongs.isNotEmpty())
        {
            if (publicSongIndex >= publicSongs.size) publicSongIndex = 0
            val s = publicSongs[publicSongIndex++]
            s.isTemp = true
            mediaList.current = s
            onSave()
            return s
        }
        return mediaList.current
    }

    fun nextSong(): Song?
    {
        if (mediaList.list.isNotEmpty())
        {
            return mediaList.list[0]
        }
        return null
    }

    @JvmStatic
    @Synchronized
    fun isSelected(song: Song): String
    {
        if (mediaList.current?.id == song.id)
        {
            song.flagVisibility.set(true)
            song.btnDeleteVisibility.set(false)
            song.btnDingVisibility.set(false)
            return "播放中"
        }

        if (mediaList.list.contains(song))
        {
            song.flagVisibility.set(true)
            song.btnDeleteVisibility.set(true)
            val index = mediaList.list.indexOf(song)
            song.btnDingVisibility.set(index > 0)
            return "已选${index + 1}"
        }
        song.flagVisibility.set(false)
        song.btnDeleteVisibility.set(false)
        song.btnDingVisibility.set(true)
        return ""
    }

    @JvmStatic
    @Synchronized
    fun isUp(song: Song) = mediaList.list.indexOf(song) == 0

    @JvmStatic
    @Synchronized
    fun index(song: Song): String
    {
        val index = mediaList.list.indexOf(song) + 1
        return "$index"
    }
}